package com.bai.serviceOA.util;

import com.bai.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;


public class MenuHelper {

    //递归方法构建菜单
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
        //创建list,用于最终数据
        List<SysMenu> trees = new ArrayList<>();
        //把所有的菜单数据进行变量
        for (SysMenu sysMenu : sysMenuList) {
            //递归入口进入   paretId=0
            if (sysMenu.getParentId().longValue() == 0){
                trees.add(getChildren(sysMenu,sysMenuList));
            }
        }
        return trees;
    }

    /**
     * 递归
     * @param sysMenu
     * @param sysMenuList
     * @return
     */
    private static SysMenu getChildren(SysMenu sysMenu, List<SysMenu> sysMenuList) {
        sysMenu.setChildren(new ArrayList<SysMenu>());
        //便利所有菜单数据，判断id和partentId对应关系
        for (SysMenu it : sysMenuList) {
            if (sysMenu.getId().equals(it.getParentId())){
                if (sysMenu.getChildren()==null){
                    sysMenu.setChildren(new ArrayList<SysMenu>());
                }
                sysMenu.getChildren().add(getChildren(it,sysMenuList));
            }
        }
        return sysMenu;
    }
}
